---
layout: "default"
title: "Encodable"
description: "Swift documentation for 'Encodable': A type that can encode itself to an external representation."
keywords: "Encodable,protocol,swift,documentation,encode"
root: "/v4.2"
---

<div class="intro-declaration"><code class="language-swift">protocol Encodable</code></div>

<div class="discussion comment">
    <p>A type that can encode itself to an external representation.</p>
</div>

<table class="standard">
<tr>
<th id="inheritance">Inheritance</th>
<td>
<span class="viz"><a href="hierarchy/">View Protocol Hierarchy &rarr;</a></span>
</td>
</tr>



<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>







<h3>Instance Methods</h3>
<div class="declaration" id="func-encode-to_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-encode-to_">func encode(<wbr>to:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-encode-to_"><div class="p">
    <p>Encodes this value into the given encoder.</p>

<p>If the value fails to encode anything, <code>encoder</code> will encode an empty
keyed container in its place.</p>

<p>This function throws an error if any values are invalid for the given
encoder&#39;s format.</p>

<p><strong><code>encoder</code>:</strong>  The encoder to write data to.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func encode(to encoder: Encoder) throws</code>
    
    
</div></div>
</div>


